<template>
  <el-dialog title="分配权限" :visible="isAssign" @close="hClose">
    <el-tree
      ref="tree"
      :data="permList"
      :props="{ label: 'name' }"
      default-expand-all
      check-strictly
      show-checkbox
      node-key="id"
    />
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="hSave">保存</el-button>
      <el-button @click="hClose">取消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getPermission } from '@/api/permission'
import { arrTree } from '@/utils'
import { getExisting, getSave } from '@/api/role'
import { Message } from 'element-ui'
export default {
  props: {
    isAssign: {
      type: Boolean,
      required: true
    },
    assignId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      permList: []
    }
  },
  created() {
    this.getAssign()
  },
  methods: {
    // 保存
    async hSave() {
      // 获取 keys 的值 通过 getCheckedKeys 方法可以获取 tree 上面的key值
      const keys = this.$refs.tree.getCheckedKeys()
      console.log(keys)
      // 发请求 获取修改数据
      await getSave({ id: this.assignId, permIds: keys })
      // 提示用户
      Message.success('修改权限成功')
      // 关闭弹窗
      this.hClose()
    },
    // 获取权限详情回显
    async getExisting() {
      const res = await getExisting(this.assignId)
      //   console.log(res.data.permIds)
      // 数据回显
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 获取权限详情
    async getAssign() {
      const res = await getPermission()
      //   console.log(res.data)
      this.permList = arrTree(res.data, 0)
    },
    // 关闭弹窗
    hClose() {
      this.$emit('update:isAssign', false)
    }
  }
}
</script>
